/**
 * 我的笔记
 * @project readnote
 * @Title BookAction.java
 * @author huxh
 * @date 2012-2-12
 **/
package com.readnote.center;

import java.util.ArrayList;
import java.util.List;

import com.cr.backbone.action.AppBaseActionSupport;
import com.cr.backbone.hibenate.Pagination;
import com.readnote.book.model.UserBook;
import com.readnote.book.model.UserNote;
import com.readnote.book.service.BookService;
import com.readnote.book.service.UserBookService;
import com.readnote.book.service.UserNoteService;
import com.readnote.user.model.User;

public class UserNoteAction extends AppBaseActionSupport{
	
	
	private UserBook userBook;
	private UserNote userNote;
	private List<UserNote> userNotes;
	private BookService bookService;
	private UserBookService userBookService;
	private UserNoteService userNoteService;
	private User user;
	private List<UserBook> userBooks=new ArrayList();
	private boolean showAll=false;
	
	
	public String index(){
//		if(userBook ==null || userBook.isNullRow())
//			userNotes = userNoteService.getListUserNoteByUser(user);
//		else
//			userNotes = userNoteService.getListUserNoteByUserBook(userBook);
		
		if(!isShowAll()){
			//1、 当前登录用户笔记
			User user=getLoginInfo().getUser();
			Long userId=user ==null || user.isNullRow()?null:user.getId();
			getQuery().addQuery("eq_user.id_l", userId);
		}
		//2、 从某书籍跳转进入，获取书籍下笔记
		Long userBookId=userBook ==null || userBook.isNullRow()?null:userBook.getId();
		if(userBookId !=null)
			getQuery().addQuery("eq_userBook.id_l", userBookId);
		page.setQueryPageTotalRows(Pagination.SHOW_PAGE_ROWS_10);
		getQuery().addOrderDesc("id");
		page=this.userNoteService.getPage(getQuery(), page);
		
//		Integer i=0;
//		for(UserNote userNote :userNotes){
//			System.out.println(i+"\t"+userNote.getTitle());
//			i++;
//		}
		return INDEX;
	}
	

	
	public String input(){
		user = getLoginInfo().getUser();
		if(userNote!=null && !userNote.isNullRow()){
			userNote = userNoteService.get(userNote.getId());
			userBook = userBookService.get(userNote.getUserBook().getId());
		}
		if(userBook!=null && !userBook.isNullRow())
			userBook = userBookService.get(userBook.getId());
		
		userBooks=userBookService.getListUserBookByUser(user);
		
		return INPUT;
	}
	
	public String view(){
		
		user = getLoginInfo().getUser();
		if(userNote!=null && !userNote.isNullRow()){
			userNote = userNoteService.get(userNote.getId());
			userBook = userBookService.get(userNote.getUserBook().getId());
		}
		if(userBook!=null && !userBook.isNullRow())
			userBook = userBookService.get(userBook.getId());
//		test html2text
//		String _content=com.cr.backbone.util.UtilsWebFormatter.html2text(userNote.getContent());
//		System.out.println(_content);
		return VIEW;
	}
	
	public String save(){
		if(userNote.isNullRow()){
			System.out.println("User Action Save :"+userNote.getTitle());
			//添加笔记
			userNoteService.saveAddUserNote(userNote);
		}else {
			
			//更新笔记信息
			UserNote _userNote=userNoteService.get(userNote.getId());;
			_userNote.setTitle(userNote.getTitle());
			_userNote.setContent(userNote.getContent());
			_userNote.setPageNumber(userNote.getPageNumber());
			_userNote.setUserBook(userNote.getUserBook());
			userNoteService.updateUserNote(_userNote);
		}
		return SUCCESS;
	}
//	
//	public String delete(){
//		userNoteService.delete(userNote);
//		return SUCCESS;
//	}

	public UserBook getUserBook() {
		return userBook;
	}

	public void setUserBook(UserBook userBook) {
		this.userBook = userBook;
	}

	public UserNote getUserNote() {
		return userNote;
	}

	public void setUserNote(UserNote userNote) {
		this.userNote = userNote;
	}

	public List<UserNote> getUserNotes() {
		return userNotes;
	}

	public void setUserNotes(List<UserNote> userNotes) {
		this.userNotes = userNotes;
	}

	public BookService getBookService() {
		return bookService;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public UserBookService getUserBookService() {
		return userBookService;
	}

	public void setUserBookService(UserBookService userBookService) {
		this.userBookService = userBookService;
	}

	public UserNoteService getUserNoteService() {
		return userNoteService;
	}

	public void setUserNoteService(UserNoteService userNoteService) {
		this.userNoteService = userNoteService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<UserBook> getUserBooks() {
		return userBooks;
	}

	public void setUserBooks(List<UserBook> userBooks) {
		this.userBooks = userBooks;
	}

	public boolean isShowAll() {
		return showAll;
	}

	public void setShowAll(boolean showAll) {
		this.showAll = showAll;
	}
	
	
	
}
